package com.winit.select.service;

import com.winit.common.util.Constants;
import com.winit.config.model.Config;
import com.winit.config.service.IConfigService;
import com.winit.indexing.model.Indexing;
import com.winit.indexing.model.IndexingId;
import com.winit.indexing.repository.IIndexingRepository;
import com.winit.indexing.service.IIdexingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by wangjian on 2017/11/6.
 */
@Component
public class SelectService {
    private static Logger log = LoggerFactory.getLogger(SelectService.class);

    @Autowired
    private IIdexingService idexingService;

    @Autowired
    private IConfigService configService;

    @Autowired
    private IIndexingRepository indexingRepository;

    /**
     * 根据类型获取下拉列表数据
     * 1、根据类型到index中查询config id
     * 2、根据id到config中查询记录
     *
     * @param types
     * @return
     */
    public Map<String, List<Map<String, String>>> getSelectConfigs(String types) {
        return getSelectConfigs(Constants.select_index_group, types);
    }

    public Map<String, List<Map<String, String>>> getSelectConfigs(String indexGroup, String types) {
        Map<String, List<Map<String, String>>> map = new HashMap();
        for (String type : types.split(",")) {
            map.put(type, getSelectConfig(indexGroup, type));
        }
        return map;
    }

    public List<Map<String, String>> getSelectConfig(String type) {
        return getSelectConfig(Constants.select_index_group, type);
    }

    public List<Map<String, String>> getSelectConfig(String indexGroup, String type) {
        List<Map<String, String>> list = new ArrayList<>();
        //1、查询index
        IndexingId id = new IndexingId();
        id.setIndexGroup(indexGroup);
        id.setFilterTag(type);
        Indexing indexing = idexingService.getIndexing(id);
        if (null == indexing) {
            return list;
        }
        String configId = null;
        for (String key : indexing.getIndexMap().keySet()) {
            configId = key;
        }

        //2、查config
        try {
            Config config = configService.getConfigMap(configId, true);
            for (String key : config.getKVMap().keySet()) {
                Map<String, String> map = new HashMap();
                if (key.contains("@@@")) {
                    map.put("key", key.split("@@@")[1]);
                    map.put("index", key.split("@@@")[0]);
                } else {
                    map.put("key", key);
                }
                map.put("value", config.getKVMap().get(key));
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("com.winit.select.service.SelectService.getSelectConfig 异常 ", e);
        }
        Collections.sort(list, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                String key1 = o1.get("key");
                String key2 = o2.get("key");
                String index1 = o1.get("index");
                String index2 = o2.get("index");
                if (!StringUtils.isEmpty(index1) && !StringUtils.isEmpty(index2)) {
                    return Integer.parseInt(index1) < (Integer.parseInt(index2)) ? -1 : 1;
                }
                if (key1.matches("^[0-9]+$") && key2.matches("^[0-9]+$")) {
                    if (Integer.parseInt(key1) < Integer.parseInt(key2)) {
                        return -1;
                    } else if (Integer.parseInt(key1) == Integer.parseInt(key2)) {
                        return 0;
                    } else {
                        return 1;
                    }
                }

                if(!StringUtils.isEmpty(index1)){
                    return -1;
                }

                return key1.compareTo(key2);
            }
        });
        return list;
    }

    /**
     * 保存下拉列表数据
     * 1
     *
     * @param type
     * @param map
     */
    public void saveSelectConfig(String type, Map map) {
        saveSelectConfig(Constants.select_index_group, type, map);
    }

    public void saveSelectConfig(String indexGroup, String type, Map map) {
        //1、查询index
        IndexingId id = new IndexingId();
        id.setIndexGroup(indexGroup);
        id.setFilterTag(type);

        Config config = new Config();
        config.setParentId("-1");

        Indexing indexing = idexingService.getIndexing(id);
        if (null == indexing) {

            config.setKVMap(map);
            config = configService.saveConfig(config);

            indexing = new Indexing();
            indexing.setId(id);
            Map<String, String> m = new HashMap<>();
            m.put(config.getId(), config.getId());
            indexing.setIndexMap(m);
            indexingRepository.save(indexing);
        } else {
            for (String key : indexing.getIndexMap().keySet()) {
                config.setId(key);
            }
            config.setKVMap(map);
            configService.saveConfig(config, false);
        }
    }
}
